package com.lzy.cloud.order.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.order.dtos.MsgOrderRefundDto;
import com.lzy.cloud.order.dtos.OrderRefundDto;
import com.lzy.cloud.order.dtos.RefundMoneyDto;
import com.lzy.cloud.order.facade.OrderRefundFacade;
import com.lzy.cloud.order.remote.BaseApi;
import com.lzy.cloud.order.request.OrderRefundRequest;
import com.lzy.cloud.order.response.MemberToken;
import com.lzy.cloud.order.response.OrderRefundResponse;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 订单退换货退款信息接口
 * @Author:   gyl
 * @Date:     2018/7/10 20:54
 */
@RestController
@RequestMapping("/order/refund")
public class OrderRefundController {

    @Autowired
    private OrderRefundFacade orderRefundFacade;

    @Autowired
    private BaseApi baseApi;

    /**
     * 方法说明：根据条件查询订单退换货记录列表（分页）
     * @date   2018/7/10 21:08
     */
    @GetMapping("/list")
    public Result queryList(OrderRefundRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<OrderRefundDto> pageRefund = orderRefundFacade.queryList(pager, DozerUtils.map(request,OrderRefundDto.class));
        if (pageRefund != null && pageRefund.getList() != null) {
            pager.setTotal(pageRefund.getTotal());
            return new Result(pager,DozerUtils.maps(pageRefund.getList(), OrderRefundResponse.class));
        } else {
            return new Result();
        }
    }
    
    /**
     * 方法说明：根据订单号查找
     * @date   2018/7/10 21:09
     */
    @GetMapping("/find/{orderId}")
    public Result find(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        OrderRefundDto bod = orderRefundFacade.find(orderId);
        return new Result(DozerUtils.map(bod, OrderRefundResponse.class));
    }

    /**
     * 方法说明：创建
     * @date   2018/7/10 21:12
     */
    @PostMapping("/create")
    public Result create(@RequestBody OrderRefundRequest request) {
        Ensure.that(request).isNotNull("00000002");
        return new Result(orderRefundFacade.create(DozerUtils.map(request,OrderRefundDto.class)));
    }

    /**
     * 方法说明：更新
     * @date   2018/7/10 21:12
     */
    @PostMapping("/update")
    public Result update(@RequestBody OrderRefundRequest request) {
        Ensure.that(request).isNotNull("00000002");
        return new Result(orderRefundFacade.update(DozerUtils.map(request,OrderRefundDto.class)));
    }

    /**
     * 逻辑退订
     * @param memberToken
     * @param orderId
     * @param orderId
     * @return
     */
    @RequestMapping(value = "logicRefund/{orderId}", method = RequestMethod.POST)
    public Result logicRefund(@RequestHeader("memberToken") String memberToken, @PathVariable("orderId") String orderId) {
        Result result = new Result();
        MemberToken mt = DozerUtils.map(baseApi.isMemberLogin(memberToken).getResult(),MemberToken.class);
        RefundMoneyDto rmDTO = new RefundMoneyDto();
        rmDTO.setMemberId(mt.getId());
        rmDTO.setMemberName(mt.getName());
        rmDTO.setOrderId(orderId);
        orderRefundFacade.exeLogicRefund(rmDTO);
        return result;
    }

}
